package com.danskin.wechat.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.danskin.wechat.dao.CustInfoExDao;
import com.danskin.wechat.dao.ProductInfoDao;
import com.danskin.wechat.entity.CustInfoEx;
import com.danskin.wechat.entity.ProductGroupon;
import com.danskin.wechat.entity.ProductInfo;
import com.danskin.wechat.service.ProductInfoService;
import com.danskin.wechat.util.Page;
@Service
public class ProductInfoServiceImpl implements ProductInfoService{
	@Autowired
    private ProductInfoDao productInfoDao; 
	@Autowired
	private CustInfoExDao  custInfoExDao;
	
	@Override
	public List<ProductInfo> findProductInfoList(Page page,String type) {
		// TODO Auto-generated method stub
		if("mall".equals(type)){
			return productInfoDao.listPageProductInfoMallList(page);
		}else{
			return productInfoDao.listPageProductInfoList(page);
		}	
	}

	@Override
	public void saveProductInfoModel(ProductInfo productInfo,String type) {
		// TODO Auto-generated method stub
		if("mall".equals(type)){
			productInfoDao.saveProductInfoModelMall(productInfo);
		}else{
			productInfoDao.saveProductInfoModel(productInfo);
		}
	}

	@Override
	public void deleteProductInfo(String id) {
		// TODO Auto-generated method stub
		productInfoDao.deleteProductInfo(id);
	}

	@Override
	public void updateProductInfo(ProductInfo productInfo,String type) {
		// TODO Auto-generated method stub
		if("mall".equals(type)){
			productInfoDao.updateProductInfoMall(productInfo);
		}else{
			productInfoDao.updateProductInfo(productInfo);
		}
		
	}

	@Override
	public ProductInfo findProductInfobyId(String id) {
		// TODO Auto-generated method stub
		return productInfoDao.findProductInfobyId(id);
	}

	@Override
	public List<ProductInfo> queryProductInfoListApi(String appId, String name) {
		// TODO Auto-generated method stub
		return productInfoDao.queryProductInfoListApi(appId,name);
	}

	@Override
	public List<ProductInfo> findProductInfoListAPI(String appId, String openId) {
		// TODO Auto-generated method stub
		return productInfoDao.findProductInfoListAPI(appId,openId);
	}

	@Override
	public List<ProductInfo> searchProductInfoList(Page page, String integral,String title) {
		// TODO Auto-generated method stub
		Map<String, Object> map = page.getMap();
		map.put("integral", integral);
		map.put("title", title);
		return productInfoDao.listPageSearchProductInfoList(page);
	}

	@Override
	public ProductInfo getProductInfoDetail(String appId, String openId, String id) {
		// TODO Auto-generated method stub
		return productInfoDao.getProductInfoDetail(appId,openId,id);
	}

	@Override
	public List<ProductInfo> findProductInfoListType(String appId, String openId,String type) {
		// TODO Auto-generated method stub
		return productInfoDao.findProductInfoListType(appId,openId,type);
	}

	@Override
	public List<ProductInfo> queryProductInfoByModelId(List<Integer> listOrd) {
		// TODO Auto-generated method stub
		return productInfoDao.queryProductInfoByModelId(listOrd);
	}

	@Override
	public List<ProductInfo> findProductInfoByModel(int id) {
		// TODO Auto-generated method stub
		return productInfoDao.findProductInfoByModel(id);
	}


	@Override
	public List<ProductInfo> queryProductInfoClassify(Page page, String type, String ordertype,String reorder) {
		// TODO Auto-generated method stub
		Map<String, Object> map = page.getMap();
		String appId = map.get("appId").toString();//分类
		String openId = map.get("openId").toString();//获取积分
		String salePoints = null;
		if(type.equals("canbeinto")){
			//可兑换
			salePoints = this.getAvaPoints(appId, openId);
			type = "";
		}else if(type.equals("all")){
			type = "";
		}else{
			type = getFormatType(type);
		}
		String time = null;
		String saly = null;
		String salePoint = null;
		String asc = null;
		//判断商城二级分类列表
		if(ordertype.equals("achievement")){
			//销量
			saly = "achievement";
		}else if(ordertype.equals("tamp")){
			//新品
			 time = "descTime";
		}else if(ordertype.equals("score")){
			//积分
			if("".equals(reorder)){
				salePoint = "jf";
			}else{
				asc = "asc";
			}
		}else{
			
		}
		map.put("type", type);
		map.put("salePoints", salePoints);
		map.put("time", time);
		map.put("saly", saly);
		map.put("salePoint", salePoint);
		map.put("asc", asc);
		return productInfoDao.listPageQueryProductInfoClassify(page);
	}
	/***
	 * 积分商城商品类型转换
	 * @param str
	 * @return
	 */
	public String getFormatType(String str){
		String type = null;
		if(str.equals("entity")){
			type = "2";
		}
        if(str.equals("coupon")){
        	type = "1";
		}
        return type;
	}
	/***
	 * 获取会员可用积分
	 * @param appId
	 * @param openId
	 * @return
	 */
	public String getAvaPoints(String appId,String openId){
		CustInfoEx custInfoEx = custInfoExDao.queryCustInfoEx(appId,openId);
		return String.valueOf(custInfoEx.getAvaPoints());
	}

	@Override
	public List<ProductInfo> queryProductInfoHotList(String appId, int num) {
		// TODO Auto-generated method stub
		return productInfoDao.queryProductInfoHotList(appId,num);
	}

	@Override
	public List<ProductInfo> queryProductInfoBySku(String sku,String type) {
		// TODO Auto-generated method stub
		return productInfoDao.queryProductInfoBySku(sku,type);
	}

	@Override
	public List<ProductInfo> queryProductInfoByModle(List<Integer> listId) {
		// TODO Auto-generated method stub
		return productInfoDao.queryProductInfoByModle(listId);
	}

	@Override
	public ProductInfo queryProductInfoDetailed(String appId, String id) {
		// TODO Auto-generated method stub
		return productInfoDao.queryProductInfoDetailed(appId,id);
	}

	@Override
	public List<ProductInfo> queryProductInfoType(int id) {
		// TODO Auto-generated method stub
		return productInfoDao.queryProductInfoType(id);
	}

	@Override
	public List<ProductInfo> queryProductInfoBySort(String sort, int mpId) {
		// TODO Auto-generated method stub
		return productInfoDao.queryProductInfoBySort(sort,mpId);
	}
}
